package org.oak.controller;

import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * org.oak.controller.RedisExampleController
 *
 * @author ZRS
 * @version V1.0
 * @since 2025/10/29 19:19
 */
@RestController
public class RedisExampleController {

    @Resource
    ListOperations<String, Object> listOperations;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    public static final String NO_KEY = "today:no:";

    /**
     * 排号
     *
     * @return
     */
    @GetMapping("/leftPush")
    public Map<String, Object> leftPush() {

        long no = stringRedisTemplate.opsForValue().increment("No", 1);
        String noStr = "No" + String.format("%04d", no);

        listOperations.leftPush(NO_KEY, noStr);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("data", noStr);
        return map;
    }


    /**
     * 叫号
     *
     * @return
     */
    @GetMapping("/rightPop")
    public Map<String, Object> rightPop() {
        Object no = listOperations.rightPop(NO_KEY);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("data", no);
        return map;
    }
}
